//package com.huiyou.adminapi.common;
//
//
//import com.alibaba.fastjson.JSONObject;
//import com.huiyou.adminapi.common.utils.DateUtil;
//
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.boot.web.client.RestTemplateBuilder;
//import org.springframework.http.HttpEntity;
//import org.springframework.http.HttpHeaders;
//import org.springframework.http.MediaType;
//import org.springframework.http.ResponseEntity;
//import org.springframework.stereotype.Component;
//import org.springframework.web.client.RestTemplate;
//
//import sun.misc.BASE64Encoder;
//
//import java.io.UnsupportedEncodingException;
//import java.net.URLEncoder;
//import java.nio.charset.StandardCharsets;
//import java.security.InvalidKeyException;
//import java.security.Key;
//import java.security.NoSuchAlgorithmException;
//import java.util.HashMap;
//import java.util.Map;
//
//import javax.crypto.Mac;
//import javax.crypto.spec.SecretKeySpec;
//
//import lombok.extern.slf4j.Slf4j;
//
//@Slf4j
////@Component
//public class DemoHttpClient {
//
//    private RestTemplate restTemplate;
//
//    //云市场分配的密钥Id
//    private final static String secretId = "AKIDh20wlGEEZKDe5vU4m0HVVh8XlO79rdNKNJ9s";
//    //云市场分配的密钥Key
//    private final static String secretKey = "KPwVt9673VN1EEAPEoOJ8105aDuQF86c9e3zpX4";
//    private final static String source = "huiyou";
//
//    private final static String URI = "https://service-53e769xh-1255468759.ap-shanghai.apigateway.myqcloud.com/release/iparea";
//
//    @Autowired
//    public DemoHttpClient(RestTemplateBuilder builder) {
//        this.restTemplate = builder.build();
//    }
//
//    public JSONObject test(String ip) {
//        Map<String, String> map = new HashMap<>();
//        map.put("ip", ip);
//
//        HttpEntity<String> request = new HttpEntity<>(map.toString(),
//                                                      getHttpHeaders(source, getSignature(source, secretKey, secretId)));
//
//        ResponseEntity<JSONObject> json = restTemplate.postForEntity(URI, request, JSONObject.class);
//        return json.getBody();
//    }
//
//    /*
//     * @return
//     */
//    private HttpHeaders getHttpHeaders(String source, String auth) {
//        HttpHeaders headers = new HttpHeaders();
//        headers.set("X-Source", source);
//        headers.set("X-Date", DateUtil.dateTime());
//        headers.set("Authorization", auth);
//        headers.setConnection(MediaType.APPLICATION_JSON_UTF8_VALUE);
//        return headers;
//    }
//
//    private String getSignature(String source, String secretKey, String secretId) {
//
//        String signStr = "x-date: " + DateUtil.dateTime() + "\n" + "x-source: " + source;
//        Mac mac = null;
//        try {
//            mac = Mac.getInstance("HmacSHA1");
//        } catch (NoSuchAlgorithmException e) {
//            e.printStackTrace();
//        }
//        Key sKey = new SecretKeySpec(secretKey.getBytes(StandardCharsets.UTF_8), mac.getAlgorithm());
//        try {
//            mac.init(sKey);
//        } catch (InvalidKeyException e) {
//            e.printStackTrace();
//        }
//        byte[] hash = mac.doFinal(signStr.getBytes(StandardCharsets.UTF_8));
//        String sig = new BASE64Encoder().encode(hash);
//
//        return "hmac id=\"" + secretId + "\", algorithm=\"hmac-sha1\", headers=\"x-date x-source\", signature=\"" + sig + "\"";
//    }
//
////    private static String urlEncode(Map<String, ?> map) {
////        StringBuilder sb = new StringBuilder();
////        map.forEach((key, value) -> {
////            if (sb.length() > 0) {
////                sb.append("&");
////            }
////            try {
////                sb.append(String.format("%s=%s",
////                                        URLEncoder.encode(key, "UTF-8"),
////                                        URLEncoder.encode(value.toString(), "UTF-8")));
////            } catch (UnsupportedEncodingException e) {
////                log.error("url encode fail");
////            }
////        });
////        return sb.toString();
////    }
//}
